package com.beaut.project.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beaut.project.dao.FlowMapper;
import com.beaut.project.dao.GroupMapper;
import com.beaut.project.util.Utility;
import com.google.gson.Gson;


@Transactional(rollbackFor = Exception.class)
@Service("groupService")
public class GroupService {
	
	@Autowired
	private GroupMapper groupMapper;
	@Autowired
	private FlowMapper flowMapper;
	
	public String listDepartment(){
		StringBuffer sb = new StringBuffer("[");
		List<Map<String,String>> rootList = groupMapper.getDepartList();
		if(rootList != null && rootList.size() > 0){
			for(int i=0;i<rootList.size();i++){
				Map<String,String> m = rootList.get(i);
				if(i != 0 && i!= rootList.size()){
					sb.append(",");
				}
				sb.append("{\"id\":\"").append(m.get("id"))
				.append("\",\"text\":\"").append(m.get("name"))
				.append("\",\"attributes\":"+Utility.toJson(m))
				.append(",\"children\":[");
				m.put("parent_id", m.get("id"));
				sb.append(getChildrenDepartSys(m)).append("]}");
			}
		}
		sb.append("]");
		return sb.toString();
	}
	private String getChildrenDepartSys(Map<String,String> map){
		StringBuffer sb = new StringBuffer();
		List<Map<String,String>> nodeList = groupMapper.getRootDepartList(map);
		if(nodeList != null && nodeList.size() >0){
			for(int i=0;i<nodeList.size();i++){
				if(i != 0 && i!= nodeList.size()){
					sb.append(",");
				}
				Map<String,String> m = nodeList.get(i);
				sb.append("{\"id\":\"").append(m.get("id"))
				.append("\",\"text\":\"").append(m.get("name"))
				.append("\",\"attributes\":"+Utility.toJson(m))
				.append(",\"children\":[");
				map.put("parent_id", m.get("id"));
				sb.append(getChildrenDepartSys(map)).append("]}");
			}
			return sb.toString();
		}
		else{
			return "";
		}
	}
	
	public String addDepartNode(Map<String, String> map) {
		Map<String,Object> parentMap = groupMapper.getDepartNodeById(map.get("parent_id"));
		map.put("tree_code", String.valueOf(parentMap.get("tree_code"))+","+String.valueOf(map.get("id")));
		groupMapper.addDepartNode(map);
		this.addOaMapDept(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateDepartNode(Map<String, String> map) {
		//同时更新本节点以及下级子节点的treecode
    	Map<String,Object> parentMap = groupMapper.getDepartNodeById(map.get("parent_id"));
		map.put("tree_code", String.valueOf(parentMap.get("tree_code"))+","+String.valueOf(map.get("id")));
		groupMapper.updateDepartNode(map);
		updateDepartTreeCode(map.get("id"),map.get("tree_code"));
		this.updateOaMapDept(map);
		return Utility.rtJson(Utility.RETURN_OK, "修改成功", null);
	}
	private void updateDepartTreeCode(String parent_id,String parent_tree_code){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("parent_id", parent_id);
		List<String> childList = groupMapper.getDeptChildren(map);
		if(childList != null && childList.size() > 0){
			for(int i=0;i<childList.size();i++){
				Map<String,Object> m = new HashMap<String,Object>();
				String id = childList.get(i);
				m.put("id", id);
				m.put("tree_code", parent_tree_code+","+id);
				groupMapper.updateDepartTreeCode(m);
				updateDepartTreeCode(id,String.valueOf(m.get("tree_code")));
			}
		}
	}
	public String deleteDepartNode(List<String> list){
		groupMapper.deleteDepartNode(list);
		this.delOaDept(list);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String listPagePriv(Map<String,Object> map){
		List<Map<String,String>> privList= groupMapper.listPagePriv(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.toJson(privList));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String addPriv(Map<String,Object> map){	
		groupMapper.addPriv(map);
		this.addOaMapPriv(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String deletePriv(Map<String,String> map){
		List<String> list = Arrays.asList(map.get("ids").split(","));
		groupMapper.deletePriv(list); 
		this.delOaPriv(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String updatePriv(Map<String,Object> map){
		groupMapper.updatePriv(map);
		this.updateOaPriv(map);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String getPrivMenuTree(String priv){
		StringBuffer sb = new StringBuffer("[");
		Map<String,Object> privMap = groupMapper.getPrivById(priv);
		Map<String,Object> queryMap = new HashMap<String,Object>();
		queryMap.put("parent_id", "0");
		sb.append(getMySysMenuTotalTree(queryMap,privMap));
		sb.append("]");
		return sb.toString();
	}
	public String initDataPriv(Map<String,Object> map){
		List<Map<String,Object>> list = groupMapper.getDataPriv(map);
		if(list != null && list.size() > 0){
		}else{
			List<Map<String,Object>> l = new ArrayList<Map<String,Object>>();
			map.put("master_id", map.get("master_id"));
			map.put("slave_id", map.get("master_id"));
			l.add(map);
			groupMapper.addDataPriv(l);
		}
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String getDataPriv(Map<String,Object> map){
		return Utility.lstMaptoJson(groupMapper.getDataPriv(map));
	}
	public String updateDataPriv(Map<String,Object> map){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		String[] slaves = String.valueOf(map.get("slaves")).split(";");
		for(String slave:slaves){
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("master_id", map.get("master"));
			m.put("slave_id", slave);
			list.add(m);
		}
		groupMapper.deleteDataPriv(map);
		groupMapper.addDataPriv(list);
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", null);
	}
	public String getMySysMenuTotalTree(Map<String,Object> map,Map<String,Object> privMap){
		StringBuffer sb = new StringBuffer();
		List<Map<String,Object>> list = groupMapper.getSysMenuList(map);
		if(list != null && list.size() > 0){
			for(int i=0;i<list.size();i++){
				Map<String,Object> m = list.get(i);
				if(i > 0){
					sb.append(",");
				}
				sb.append("{\"id\":\"").append(m.get("seq_id"));
				sb.append("\",\"text\":\"").append(m.get("text"));
				
				map.put("parent_id", m.get("seq_id"));
				if("0".equals(m.get("parent_id"))){
					List<Map<String,Object>> l = groupMapper.getSysMenuList(map);
					if(l != null && l.size() > 0){
						sb.append("\",\"state\":\"closed");
					}
				}
				if(hasPriv(String.valueOf(m.get("seq_id")), privMap)){
					sb.append("\",\"checked\":\"true");
				}
				sb.append("\",\"attributes\":"+Utility.toJson(m));
				
				sb.append(",\"children\":[")
				.append(getMySysMenuTotalTree(map,privMap)).append("]}");
			}
			return sb.toString();
		}else{
			return "";
		}
	}
	private boolean hasPriv(String id,Map<String,Object> privMap){
		String menu =  privMap.get("menu")==null?"":String.valueOf(privMap.get("menu"));
		List<String> ids = Arrays.asList(menu.split(","));
		if(ids!=null && ids.size()>0){
			for(int j=0;j<ids.size();j++){
				if(id.equals(ids.get(j))){
					return true;
				}
			}
		}
		return false;
	}
	public String updatePrivMenu(Map<String,Object> map){
		groupMapper.updatePrivMenu(map);
		return Utility.rtJson(Utility.RETURN_OK, "修改成功", null);
	}
	public String listPageUser(Map<String,Object> map){
		List<Map<String,Object>> users= groupMapper.listPageUser(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(users));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPageCompany(Map<String,Object> map){
		List<Map<String,Object>> company= groupMapper.listPageCompany(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(company));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String listPriv(){
		List<Map<String,Object>> list = groupMapper.listPriv();
		return Utility.lstMaptoJson(list);
	}
	public String getUserById(Map<String,Object> map){
		return Utility.toJson(groupMapper.getUserById(map));
	}
	
	
	public String addUser(Map<String,Object> map){
		Map<String,Object> m = groupMapper.getUserByUserName(map);
//		if(m != null){
//			return Utility.rtJson(Utility.RETURN_ERROR, "该用户名称已存在", null);
//		}
		groupMapper.addUser(map);
		groupMapper.addUserDept(map);
	 	groupMapper.addUserPriv(map);
	    this.addOaMapPerson(map);
	    //添加自己的数据权限
	    map.put("master_id", map.get("user_id"));
	    initDataPriv(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String addCompany(Map<String,Object> map){
		map.put("id", "");
		Map<String,Object> m = groupMapper.getCompanyByNameOrId(map);
		
		String company_id=String.valueOf(UUID.randomUUID());
		map.put("company_id", company_id);
		map.put("is_del", 0);
		if(m != null){
			return Utility.rtJson(Utility.RETURN_ERROR, "该公司名称已存在", null);
		}
		groupMapper.addCompany(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String getCompanyByNameOrId(Map<String,Object> map){
		return Utility.toJson(groupMapper.getCompanyByNameOrId(map));
	}
	public String updateUser(Map<String,Object> map){
		Map<String,Object> m = groupMapper.getUserByUserName(map);
		//相同用户名不同id，表示本次修改的用户名已经存在
		if(m != null){
			if(!String.valueOf(m.get("id")).equals(String.valueOf(map.get("id")))){
				return Utility.rtJson(Utility.RETURN_ERROR, "该用户名称已存在", null);
			}
		}
		groupMapper.updateUser(map);
		groupMapper.deleteUserDept(map);
		groupMapper.deleteUserPriv(map);
		map.put("user_id", map.get("id"));
		groupMapper.addUserDept(map);
		groupMapper.addUserPriv(map);
		this.updateOaMapPerson(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String updateCompany(Map<String,Object> map){
		Map<String,Object> m = groupMapper.getCompanyByNameOrId(map);
		//相同用户名不同id，表示本次修改的用户名已经存在
		if(m != null){
			return Utility.rtJson(Utility.RETURN_ERROR, "该用户名称已存在", null);
		}
		groupMapper.updateCompany(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteCompany(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		groupMapper.deleteCompany(list); 
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteUser(Map<String,String> map){
		List<String> list = Arrays.asList(map.get("ids").split(","));
		groupMapper.deleteUser(list); 
		this.flowMapper.deletePerson1(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String deleteUser1(Map<String,String> map){
		List<String> list = Arrays.asList(map.get("ids").split(","));
		groupMapper.deleteUser1(list);
		this.flowMapper.deletePerson1(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String getUnit(){
		return Utility.lstMaptoJson(groupMapper.getUnit());
	}
	public String listPageUnit(Map<String,Object> map){
		List<Map<String,Object>> units= groupMapper.listPageUnit(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(units));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String addUnit(Map<String,Object> map){
		map.put("id", UUID.randomUUID().toString());
		groupMapper.addUnit(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateUnit(Map<String,Object> map){
		groupMapper.updateUnit(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteUnit(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		groupMapper.deleteUnit(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String getSystemTypeTree(String parent_id,String type){
		StringBuffer sb = null;
		if("0".equals(parent_id)){
			sb = new StringBuffer("[");
		}else{
			sb = new StringBuffer();
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", type);
		map.put("parent_id", parent_id);
		List<Map<String,Object>> rootList = groupMapper.getSystemTypeTree(map);
		if(rootList != null && rootList.size() > 0){
			for(int i=0;i<rootList.size();i++){
				Map<String,Object> m = rootList.get(i);
				if(i != 0 && i!= rootList.size()){
					sb.append(",");
				}
				sb.append("{\"id\":\"").append(m.get("id"))
				.append("\",\"text\":\"").append(m.get("name"))
			//	.append("\",\"attributes\":"+Utility.toJson(m))
				.append("\",\"children\":[");
//				.append(",\"children\":[");
				sb.append(getSystemTypeTree(String.valueOf(m.get("id")),type)).append("]}");
			}
		}else{
			if(!"0".equals(parent_id)){
				return "";
			}
		}
		if("0".equals(parent_id)){
			sb.append("]");
		}
		return sb.toString();
	}
	public String getSystemTypeBox(String parent_id,String type){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", type);
		map.put("parent_id", parent_id);
		List<Map<String,Object>> rootList = groupMapper.getSystemTypeBox(map);
		Gson gson = new Gson();
		return gson.toJson(rootList);
	}
	public String getSystemType(){
		List<Map<String,Object>> list = groupMapper.getSystemType();
		Gson gson = new Gson();
		StringBuffer sb = new StringBuffer("");
		sb.append("{\"total\":"+list.size()+",");
		sb.append("\"rows\":");
		sb.append(gson.toJson(list));
		sb.append("}");
		return sb.toString();
	}
	public String getSystemTypeById(Map<String,Object> map){
		return Utility.toJson(groupMapper.getSystemTypeById(map));
	}
	public String addSystemType(Map<String,Object> map){
		String id = UUID.randomUUID().toString();
		map.put("id", id);
		map.put("tree_code", String.valueOf(map.get("parent_tree_code"))+","+id);
		groupMapper.addSystemType(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateSystemType(Map<String,Object> map){
		groupMapper.updateSystemType(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteSystemType(Map<String,Object> map){
		groupMapper.deleteSystemType(map);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	
	public String updateOaMapPerson(Map<String,Object> map){
//	 this.flowMapper.addPerson(map);
		map.put("department_id", map.get("dept_id"));
	 	Map m0=this.flowMapper.getOaDeptIdById(map);
	 	Map m1=this.flowMapper.getOaPrivIdByUserPrivId(map);
	 	map.putAll(m0);
	 	map.putAll(m1);
		if(Utility.isNullorEmpty(String.valueOf(map.get("birthday")))){
	 		map.put("birthday", new Date());
	 	}
	 	this.flowMapper.updatePerson(map);
	 	return null;
	}
	
	
	public String addOaMapPerson(Map<String,Object> map){
	
	 	map.put("department_id", map.get("dept_id"));
	 	Map m0=this.flowMapper.getOaDeptIdById(map);
	 	Map m1=this.flowMapper.getOaPrivIdByUserPrivId(map);
	 	map.putAll(m0);
	 	map.putAll(m1);
	 	if(Utility.isNullorEmpty(String.valueOf(map.get("birthday")))){
	 		map.put("birthday", new Date());
	 	}
	 	Long seq_id=this.flowMapper.addPerson(map);
	 	seq_id=Long.valueOf(map.get("seq_id").toString());
	 	Map<String,Object> m2 =new HashMap();
	 	m2.put("user_id", map.get("user_id"));
	 	m2.put("person_id", seq_id);
	 	this.flowMapper.addMapPerson(m2);
	 	return null;
	}
	
	public String updateOaMapDept(Map<String,String> map){
		String parentId=map.get("parent_id").toString();
		/*非根节点*/
		if(!parentId.equals("0")){
			Map<String,Object> m2 = new HashMap<String,Object>();
			m2.put("department_id", parentId);
			Map<String,Object> rmap= this.flowMapper.getOaDeptIdById(m2);
			if(rmap!=null && rmap.size()>0){
				String oa_parent_id=rmap.get("oa_department_id").toString();
				Map<String,Object> m3 = new HashMap<String,Object>();
				m3.put("dept_no", map.get("sort"));
				m3.put("dept_name", map.get("name"));
				m3.put("dept_parent", oa_parent_id);
				m3.put("department_id", map.get("id"));
				this.flowMapper.updateDept(m3);
			}
		}else{
			
		}
		 	return null;
		}
	
	public String addOaMapDept(Map<String,String> map){
		String parentId=map.get("parent_id").toString();
		/*非根节点*/
		if(!parentId.equals("0")){
			Map<String,Object> m2 = new HashMap<String,Object>();
			m2.put("department_id", parentId);
			Map<String,Object> rmap= this.flowMapper.getOaDeptIdById(m2);
			if(rmap!=null && rmap.size()>0){
			String oa_parent_id=rmap.get("oa_department_id").toString();
			Map<String,Object> m3 = new HashMap<String,Object>();
			m3.put("dept_no", map.get("sort"));
			m3.put("dept_name", map.get("name"));
			m3.put("dept_parent", oa_parent_id);
			Long mapId=this.flowMapper.addOaDept(m3);
			mapId=Long.valueOf(m3.get("seq_id").toString());
			Map<String,Object> m4 = new HashMap<String,Object>();
			m4.put("department_id", map.get("id"));
			m4.put("oa_department_id",mapId);
			this.flowMapper.addMapDept(m4);
			}
		}
		else{
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("department_id", map.get("id"));
			m.put("oa_department_id", 0);
			this.flowMapper.addMapDept(m);
		}
		return null;
	}
	
	public String addOaMapPriv(Map<String,Object> map){
		Long mapId=this.flowMapper.addOaPriv(map);
		mapId=Long.valueOf(map.get("seq_id").toString());
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("priv_id", map.get("id"));
		m.put("oa_fl_priv_id",mapId);
		this.flowMapper.addMapPriv(m);
	 	return null;
	}
	public String updateOaPriv(Map<String,Object> map){

		this.flowMapper.updateOaPriv(map);
	 	return null;
	}
	public void delOaPriv(List<String> list){
		this.flowMapper.deleteOaPriv(list);
	}
	
	public void delOaDept(List<String> list){
		this.flowMapper.deleteOaDept(list);
	}
	public String addFBItem(Map<String,Object> map){
		map.put("id", UUID.randomUUID().toString());
		groupMapper.addFBItem(map);
		return Utility.rtJson(Utility.RETURN_OK, "添加成功", null);
	}
	public String updateFBItem(Map<String,Object> map){
		groupMapper.updateFBItem(map);
		return Utility.rtJson(Utility.RETURN_OK, "更新成功", null);
	}
	public String deleteFBItem(Map<String,Object> map){
		List<String> list = Arrays.asList(String.valueOf(map.get("ids")).split(","));
		groupMapper.deleteFBItem(list);
		return Utility.rtJson(Utility.RETURN_OK, "删除成功", null);
	}
	public String listPageFBItem(Map<String,Object> map){
		List<Map<String,Object>> units= groupMapper.listPageFBItem(map);
		StringBuffer sb = new StringBuffer("");
		sb.append(Utility.lstMaptoJson(units));
		Utility.putSuccess(map);
		return sb.toString();
	}
	public String getFBItem(){
		return Utility.lstMaptoJson(groupMapper.getFBItem());
	}
	public String updateGdPerson(Map<String,String> map){
		List<String> list = Arrays.asList(map.get("ids").split(","));
		for(int i=0;i<list.size();i++){
			map.put("gd_id", list.get(i));
			groupMapper.updateGdPerson(map); 
		}
		
		return Utility.rtJson(Utility.RETURN_OK, "人员变更成功", null);
	}
	public String getBusinessView(Map<String,String> map){
		return Utility.rtJson(Utility.RETURN_OK, "操作成功", Utility.toJson(groupMapper.getBusinessView(map)));
	}
}
